import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;

/**
* @description: 剑指 Offer 59 - II. 队列的最大值
* @author hewei
* @date 2022/7/23 14:07
* @version 1.0
*/

public class MaxQueue2 {

    public static void main(String[] args) {
        MaxQueue2 maxQueue2 = new MaxQueue2();
        maxQueue2.push_back(1);
        maxQueue2.push_back(2);
        System.out.println(maxQueue2.max_value());
        maxQueue2.pop_front();
        System.out.println(maxQueue2.max_value());
    }

    Queue<Integer> queue;
    Deque<Integer> maxQueue;
    public MaxQueue2() {
        queue = new LinkedList<>();
        maxQueue = new LinkedList<>();
    }

    public int max_value() {
        return maxQueue.isEmpty() ? -1 : maxQueue.peekFirst();
    }

    public void push_back(int value) {
        queue.offer(value);
        while (!maxQueue.isEmpty() && maxQueue.peekLast() < value) maxQueue.pollLast();
        maxQueue.offerLast(value);
    }

    public int pop_front() {
        if (queue.isEmpty()) return -1;
        Integer poll = queue.poll();
        if (poll.equals(maxQueue.peekFirst())) maxQueue.pollFirst();
        return poll;
    }
}
